if /I "%1" == "-h" (
	call :usage
	exit /B 1
)

if not exist "!USERPROFILE!" (
	echo Can't found home path "(USERPROFILE)"
	exit /B 1
)

if exist "!USERPROFILE!\.jenny" (
	echo Jenny was already installed. Use "jenny update" for updating.
	exit /B 1
)

if /I "%1" == "" (
	call common\function.bat extract_git_url j_jenny_git_url	
) else (
	set j_jenny_git_url=%1
)

if /I "!j_jenny_git_url!" == "" (
	echo Failed to extract jenny git url. Please try "jenny install git_url"
	exit /B 1
)

echo "Git url is:" !j_jenny_git_url!

md !USERPROFILE!\.jenny\jenny
if not !ERRORLEVEL! EQU 0 (
	echo Failed to create folder !USERPROFILE!\.jenny\jenny
	goto :die_install
)
:: in case git is a cmd an quit unexpected
cmd /E:ON /V:ON /C git clone !j_jenny_git_url! !USERPROFILE!\.jenny\jenny 

if not !ERRORLEVEL! EQU 0 (
	echo Failed to clone jenny with url: !j_jenny_git_url!
	goto :die_install
)
cmd /E:ON /V:ON /C "!USERPROFILE!\.jenny\jenny\jenny.bat" update
if not !ERRORLEVEL! EQU 0 (
	echo Failed to update during installation
	goto :die_install
)


exit /B 0

:usage
	echo jenny install [-h] [git url]
	echo     install jenny.
	echo     -h                print this help.
	echo     [git url]         install jenny, use given url, or use the remote url of this git project.
	exit /B 1

:die_install
	rd /s /q !USERPROFILE!\.jenny
	exit /B 1
